package com.nhncorp.neptune.client;

import java.util.Arrays;
import java.util.TreeSet;

import junit.framework.TestCase;

public class TestKeys extends TestCase {
  public void testComparable() {
    Row.Key key1 = new Row.Key("1786963518");
    Row.Key key2 = new Row.Key("1786963518");
    
    Row.Key key3 = new Row.Key("1786963517");
    
    assertTrue(key1.equals(key1));
    assertEquals(0, key1.compareTo(key1));

    assertTrue(key1.equals(key2));
    assertTrue(key2.equals(key1));
    assertEquals(0, key1.compareTo(key2));
    assertEquals(0, key2.compareTo(key1));
    
    assertTrue(key1.compareTo(key3) > 0);
    assertTrue(key2.compareTo(key3) > 0);
    assertTrue(key3.compareTo(key1) < 0);
    assertTrue(key3.compareTo(key2) < 0);
  }
  
  public void testEmptyKey() {
    Cell.Key key1 = Cell.Key.EMPTY_KEY;
    Cell.Key key2 = Cell.Key.EMPTY_KEY;
    
    assertTrue(key1.equals(key1));
    assertEquals(0, key1.compareTo(key1));

    assertTrue(key1.equals(key2));
    assertTrue(key2.equals(key1));
    assertEquals(0, key1.compareTo(key2));
    assertEquals(0, key2.compareTo(key1));
  }
  
  public void testNullInstance() {
    try {
      new Row.Key("1786963518").compareTo(null);
      assertTrue(false);
    } catch(NullPointerException e) {
    } catch(Exception e) {
      e.printStackTrace();
      assertTrue(false);
    }
  }
  
  public void testReusedKeys() {
    Row.Key key1 = new Row.Key("1786963518");
    Row.Key key2 = new Row.Key("aaaaaaaaaaaaaaa");
    
    key1.set(new Row.Key("1786"));
    key2.set(new Row.Key("1786"));
    
    assertFalse(Arrays.equals(key1.getBytes(), key2.getBytes()));
    
    assertTrue(key1.equals(key2));
    assertTrue(key2.equals(key1));
    assertEquals(0, key1.compareTo(key2));
    assertEquals(0, key2.compareTo(key1));
  }
  
  public void testTreeSet() {
    TreeSet<Row.Key> rowKeySet = new TreeSet<Row.Key>();

    assertTrue(rowKeySet.add(new Row.Key("1786")));
    assertEquals(1, rowKeySet.size());
    assertFalse(rowKeySet.add(new Row.Key("1786")));
    assertEquals(1, rowKeySet.size());
  }
}
